package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Lost;
import cn.bdqn.t354.service.LostService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/lost")
public class LostController {
    @Resource
    private LostService lostService;

    //客户流失预警分页查询，跳转到流失列表页面
    @RequestMapping("/list")
    public String list(String custName,String managerName,String status, Model model,
                       @RequestParam(defaultValue = "1")Integer pageIndex){

        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Lost> lostPager=lostService.getLostsByPage(custName,managerName,status,pageIndex, pageSize);
        model.addAttribute("lostPager", lostPager);

        //查询条件回显
        model.addAttribute("custName", custName);
        model.addAttribute("managerName", managerName);
        model.addAttribute("status", status);

        return "lost/list";
    }

    /**
     * 跳转暂缓流失页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/addDefer")
    public String addDefer(Long id,Model model){
        Lost lost=lostService.getLost(id);
        model.addAttribute("lost",lost);
        return "lost/defer";
    }

    /**
     * 保存暂缓流失数据
     * @param lost
     * @param model
     * @return
     */
    @RequestMapping("/saveDefer")
    public String saveDefer(Lost lost,Model model){
        lost.setLstStatus("2");//修改为暂缓流失
        try {
            lostService.updateLost(lost);
            return "redirect:/lost/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/lost/addDefer";
        }
    }

    /**
     * 跳转确认流失页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/addConfirm")
    public String addConfirm(Long id,Model model){
        Lost lost=lostService.getLost(id);
        model.addAttribute("lost",lost);
        return "lost/confirm";
    }

    /**
     * 保存确认流失数据
     * @param lost
     * @param model
     * @return
     */
    @RequestMapping("/saveConfirm")
    public String saveConfirm(Lost lost,Model model){
        lost.setLstStatus("3");//修改为确认流失
        lost.setLstLostDate(new Date());//确认流失时间
        try {
            lostService.updateLost(lost);
            return "redirect:/lost/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/lost/addConfirm";
        }
    }
}
